/**
 * Title:        流程工具检测
 * Company:      泛微软件
 */


package checkmain;

import general.BaseBean;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import checkDetail.CheckBase;
import weaver.system.ThreadWorkTimer;
import java.lang.reflect.Field;
import weaver.system.WrokflowOverTimeTimer;
import weaver.workflow.request.WorkflowAgentOp;
import weaver.workflow.workflow.SysWorkFlowAlert;
import weaver.general.AutoWorlflowThread;
import weaver.general.Util;

public class CheckExucute extends BaseBean {


    private List<String> classnameList;
    private int type;
    private List<HashMap> dataMapList;
    /**
     * 构造函数
     * @param classname class名列表
     * @param type 操作类型列表：0：检查；1：修复
     * @param InfoName 输出信息名称列表
     * @param dataMapList 其他
     */
    public CheckExucute() {
        this.classnameList = new ArrayList<String>();
        this.type = -1;
        this.dataMapList = new ArrayList<HashMap>();
    }
    
    public String execute(List<String> iClassnameList,Integer iType,List<HashMap> iDataMapList){
        this.classnameList = (List<String>)iClassnameList;
        this.type = iType;
        this.dataMapList = iDataMapList;

        JSONArray resultArray = new JSONArray();
        String msgStr = "";
        List<String> resultList = new ArrayList<String>();
        for(int i = 0; i < classnameList.size() ; i++){
            String classname = classnameList.get(i);


            //判断InfoNameList列表长度
            HashMap dataMap = new HashMap();
            if(dataMapList.size() <= i){
                msgStr = "--dataMapList 长度不匹配------------------------------>";
                writeLog(msgStr);
                resultArray.put(formatReturnJson(resultERROR,msgStr,null));
                continue;
            }else{
                dataMap = dataMapList.get(i);
            }
            
            CheckBase object = null;
            try {
                object = (CheckBase) Class.forName(classname).newInstance();
                if(object == null){
                    msgStr = classname + "--class找不到。------------------------------>";
                    writeLog(msgStr);
                    resultArray.put(formatReturnJson(resultERROR,msgStr,null));
                    continue;
                }
            } catch (Exception e) {
                msgStr = classname + "--class找不到。------------------------------>";
                writeLog(msgStr);
                resultArray.put(formatReturnJson(resultERROR,msgStr,null));
                e.printStackTrace();
                continue;
            }
            try {
                if(type == 0){
                    //检测
                    resultArray.put(object.checkData(dataMap));
                }else{
                    //修复
                    resultArray.put(object.fixData(dataMap));
                }
            } catch (Exception e) {
                msgStr = classname + "--执行失败。------------------------------>";
                writeLog(msgStr);
                resultArray.put(formatReturnJson(resultERROR,msgStr,null));
                e.printStackTrace();
                continue;
            }
        }
        return resultArray.toString();
        
    }

    /**
     * 获取同一路径下所有子类或接口实现类
     * 
     * @param intf
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static String getAllCheckClass(String path,String packagename){
        String resultStr = "";
        JSONArray jsonArray=new JSONArray();
        try {
            String thispath = path + "/classes/" + packagename;
            //根据文件路径获取文件
            File xFile=new File(thispath);
            ClassLoader classLoader = new URLClassLoader(new URL[]{new File(path + "/classes").toURI().toURL()},Thread.currentThread().getContextClassLoader()); 
            if(xFile.exists()){

                File[] files = xFile.listFiles();
                for(File file : files){
                    String fileName = file.getName();
                    //判断文件名字是否是class后缀，而且不是CheckBase.class
                    if(fileName.endsWith(".class") 
                            && ((packagename.equals("checkDetail") && !fileName.equals("CheckBase.class"))
                                    || (packagename.equals("checkWithCondit") && !fileName.equals("checkWithConBase.class")))){
                        fileName = fileName.substring(0,fileName.length() - 6);
                        Class xClass=classLoader.loadClass( packagename + "." + fileName);
                        Object checkInstance = xClass.newInstance();
                        Method xMethod=xClass.getDeclaredMethod("getClassName");
                        JSONObject jsonObject=new JSONObject();
                        jsonObject.put("classname", xClass.getName());
                        
                        jsonObject.put("ruleName", xMethod.invoke(checkInstance));
                        if(packagename.equals("checkWithCondit")){
                            jsonObject.put("classId", xClass.getName().substring(16));
                            Method xMethod2=xClass.getDeclaredMethod("getPopupUrl");
                            String jspurl = (String)xMethod2.invoke(checkInstance);
                            String linkjspurl ="<a data-toggle='modal' onclick='showModalByid(\"" + jspurl + "\",\"" + xClass.getName() + "\");' class='btn btn-primary btn-large'>弹出条件输入框</a>";
                            jsonObject.put("linkjspurl", linkjspurl);
                        }else{
                            jsonObject.put("classId", xClass.getName().substring(12));
                        }
                        //获取输入框列表
                        Method xMethod1 =xClass.getDeclaredMethod("getClassDesc");
                        jsonObject.put("classDesc", xMethod1.invoke(checkInstance));
                        
                        jsonArray.put(jsonObject);
                    }
                }
                resultStr = jsonArray.toString();
            }

            /*
            List<Class<?>> allCheckClasses = getAllAssignedClass(CheckBase.class);
            for(Class<?> c : allCheckClasses){
                Object obj = c.newInstance();
                CheckBase checkBase = (CheckBase) obj;
                JSONObject jsonObject=new JSONObject();
                jsonObject.put("classname", c.getName());
                jsonObject.put("infoName", checkBase.getClassName());
                jsonArray.put(jsonObject);
            }
            */
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultStr;
    }

}



